﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace P2PLib.Client.Command
{
    public class CCRequestFileCommand:CCCommandBase
    {
        private string fileHashCode;

        /// <summary>
        /// 无参数的构造方法里边应该初始化私有变量
        /// </summary>
        public CCRequestFileCommand() :
            base(CCCommandType.RequestFile)
        {
            fileHashCode = string.Empty;
        }

        public CCRequestFileCommand(string code)
            : base(CCCommandType.RequestFile)
        {
            fileHashCode = code;
        }


        public string FileHashCode
        {
            get
            {
                return this.fileHashCode;
            }
            set {
                this.fileHashCode = value;
            }
        }


        public override void ReadFromStream(System.IO.Stream stream)
        {
            byte[] buffer = new byte[4];
            stream.Read(buffer, 0, 4);
            int codeSize = BitConverter.ToInt32(buffer, 0);
            buffer = new byte[codeSize];
            stream.Read(buffer, 0, codeSize);
            this.fileHashCode = Encoding.ASCII.GetString(buffer);
        }

        public override void WriteToStream(System.IO.Stream stream)
        {
            byte[] buffer = Encoding.ASCII.GetBytes(this.fileHashCode);
            stream.Write(BitConverter.GetBytes(buffer.Length), 0, 4);
            stream.Write(buffer, 0, buffer.Length);
        }
    }
}
